package com.ruibang.glass.equipment.service.impl.device;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.entity.device.AccidentRegister;
//import com.ruibang.glass.equipment.entity.device.GAccidentFile;
import com.ruibang.glass.equipment.feign.FileApi;
import com.ruibang.glass.equipment.mapper.device.AccidentRegisterMapper;
import com.ruibang.glass.equipment.service.AccidentRegisterService;
//import com.ruibang.glass.equipment.service.GAccidentFileService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-11-15  09:17
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccidentRegisterServiceImpl extends ServiceImpl<AccidentRegisterMapper, AccidentRegister> implements AccidentRegisterService {

    @Autowired
    private FileApi fileApi;

    /**
     * 跟新新增
     * @param accidentRegister
     * @return
     */

    @Override
    public IPage<AccidentRegister> queryPage(DeviceAllPageReq deviceAllPageReq) {
        return baseMapper.getCheckList(new Page(deviceAllPageReq.getPageNo(), deviceAllPageReq.getPageSize()), deviceAllPageReq);
    }

    /**
     *
     * @param deviceAllPageRea
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody updateOrAddById(AccidentRegister accidentRegister) {
        if (CollectionUtils.isNotEmpty(accidentRegister.getRemoveIdList())) {
            fileApi.deleteByFileIds(accidentRegister.getRemoveIdList());
        }

        if (CollectionUtils.isNotEmpty(accidentRegister.getFilesIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setBusinessId(accidentRegister.getId());
            fileBusiness.setFileIds(accidentRegister.getFilesIds());
            fileApi.updateBusinessById(fileBusiness);
        }

        if (StringUtils.isBlank(accidentRegister.getId())) {
            //更新
            baseMapper.insert(accidentRegister);
            return ResultBody.success();
        }

        baseMapper.updateById(accidentRegister);
        return ResultBody.success();

    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public AccidentRegister getDetail(String id) {
        AccidentRegister accidentRegister = baseMapper.selectById(id);
        if (accidentRegister!=null) {
            ResultBody<List<String>> resultBody = fileApi.getFileIdsByBusinessId(id);
            if(resultBody!=null&& CollectionUtils.isNotEmpty(resultBody.getData())){
                accidentRegister.setFilesIds(resultBody.getData());
            }
        }

        return accidentRegister;
    }

    /**
     * 下载
     * @param id
     */
    @Override
    public void download(String id) {
        ResultBody<List<String>> resultBody = fileApi.getFileIdsByBusinessId(id);
        if(resultBody!=null&& CollectionUtils.isNotEmpty(resultBody.getData())){
            for (String fileId : resultBody.getData()) {
                fileApi.downloadFile(fileId);
            }
        }
    }


}
